package com.tancire.sources.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.tancire.common.exception.ResourceException;
import com.tancire.common.utils.DateUtils;
import com.tancire.common.utils.SecurityUtils;
import com.tancire.common.utils.TcStrUtils;
import com.tancire.sources.domain.SurResourceCategory;
import com.tancire.sources.mapper.SurResourceCategoryMapper;
import com.tancire.sources.service.ISurResourceCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 资源类型Service业务层处理
 *
 * @author chenchao
 * @date 2024-07-05
 */
@Service
public class SurResourceCategoryServiceImpl implements ISurResourceCategoryService {
    @Resource
    private SurResourceCategoryMapper surResourceCategoryMapper;

    /**
     * 查询资源类型
     *
     * @param id 资源类型主键
     * @return 资源类型
     */
    @Override
    public SurResourceCategory selectSurResourceCategoryById(Long id) {
        return surResourceCategoryMapper.selectSurResourceCategoryById(id);
    }

    /**
     * 查询资源类型列表
     *
     * @param surResourceCategory 资源类型
     * @return 资源类型
     */
    @Override
    public List<SurResourceCategory> selectSurResourceCategoryList(SurResourceCategory surResourceCategory) {
        return surResourceCategoryMapper.selectSurResourceCategoryList(surResourceCategory);
    }

    /**
     * 新增资源类型
     *
     * @param surResourceCategory 资源类型
     * @return 结果
     */
    @Override
    public int insertSurResourceCategory(SurResourceCategory surResourceCategory) {
        surResourceCategory.setCreateTime(DateUtils.getNowDate());
        surResourceCategory.setCreateBy(SecurityUtils.getUserId() + "");
        Long pid = surResourceCategory.getPid();
        if (null == pid) {
            throw new ResourceException("父节点信息不能为空");
        }
        if (0 == pid) {
            surResourceCategory.setCategoryLevel(1);
            surResourceCategory.setPids("");
        } else {
            SurResourceCategory parentCategory = surResourceCategoryMapper.selectSurResourceCategoryById(pid);
            if (null == parentCategory) {
                throw new ResourceException("父节点信息错误");
            }
            Integer categoryLevel = parentCategory.getCategoryLevel();
            categoryLevel++;
            surResourceCategory.setCategoryLevel(categoryLevel);
            // 拼装父节点
            String pids = TcStrUtils.joinStr(parentCategory.getPids(), parentCategory.getId() + "");
            surResourceCategory.setPids(pids);
        }
        return surResourceCategoryMapper.insertSurResourceCategory(surResourceCategory);
    }

    /**
     * 修改资源类型
     *
     * @param surResourceCategory 资源类型
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSurResourceCategory(SurResourceCategory surResourceCategory) {
        surResourceCategory.setUpdateTime(DateUtils.getNowDate());
        surResourceCategory.setUpdateBy(SecurityUtils.getUserId() + "");
        // 处理节点信息
        SurResourceCategory oldCategory = this.surResourceCategoryMapper.selectSurResourceCategoryById(surResourceCategory.getId());
        Long pid = oldCategory.getPid();
        Long nPid = surResourceCategory.getPid();
        if (null != nPid && !nPid.equals(pid)) {
            // 只有修改了父节点，才需要处理层级父节点信息
            if (null == pid || pid == 0) {
                surResourceCategory.setPid(0L);
                surResourceCategory.setPids("");
                surResourceCategory.setCategoryLevel(1);
            } else {
                SurResourceCategory parentCategory = this.surResourceCategoryMapper.selectSurResourceCategoryById(pid);
                // 处理节点数据
                Long parentId = parentCategory.getId();
                String pids = parentCategory.getPids();
                Integer categoryLevel = parentCategory.getCategoryLevel();
                surResourceCategory.setPids(TcStrUtils.joinStr(pids, parentId + ""));
                surResourceCategory.setCategoryLevel(categoryLevel + 1);
            }
            this.handleChildNode(surResourceCategory);
        }
        return surResourceCategoryMapper.updateSurResourceCategory(surResourceCategory);
    }

    /**
     * 处理子节点信息
     *
     * @param surResourceCategory
     */
    private void handleChildNode(SurResourceCategory surResourceCategory) {
        // 获取ID
        Long id = surResourceCategory.getId();
        // 查询子节点
        SurResourceCategory query = new SurResourceCategory();
        query.setPid(id);
        List<SurResourceCategory> surResourceCategories = this.surResourceCategoryMapper.selectSurResourceCategoryList(query);
        if (ObjectUtil.isNotEmpty(surResourceCategories)) {
            for (SurResourceCategory category : surResourceCategories) {
                category.setPids(TcStrUtils.joinStr(category.getPids(), category.getId() + ""));
                category.setCategoryLevel(category.getCategoryLevel() + 1);
                // 更新数据
                this.surResourceCategoryMapper.updateSurResourceCategory(category);
                // 向下查询节点
                this.handleChildNode(category);
            }
        }
    }

    /**
     * 批量删除资源类型
     *
     * @param ids 需要删除的资源类型主键
     * @return 结果
     */
    @Override
    public int deleteSurResourceCategoryByIds(Long[] ids) {
        return surResourceCategoryMapper.deleteSurResourceCategoryByIds(ids);
    }

    /**
     * 删除资源类型信息
     *
     * @param id 资源类型主键
     * @return 结果
     */
    @Override
    public int deleteSurResourceCategoryById(Long id) {
        return surResourceCategoryMapper.deleteSurResourceCategoryById(id);
    }

    /**
     * 查询一级分类
     *
     * @return
     */
    @Override
    public List<SurResourceCategory> listMainCategory() {
        SurResourceCategory query = new SurResourceCategory();
        query.setCategoryLevel(1);
        return this.surResourceCategoryMapper.selectSurResourceCategoryList(query);
    }

    /**
     * 根据父节点查询子分类
     *
     * @param pid
     * @return
     */
    @Override
    public List<SurResourceCategory> listCategoryByPid(Long pid) {
        if (null == pid) {
            return Collections.emptyList();
        }
        SurResourceCategory query = new SurResourceCategory();
        query.setPid(pid);
        return this.surResourceCategoryMapper.selectSurResourceCategoryList(query);
    }
}
